﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestGame {
    /// <summary>
    /// This Screen allows for the user to str any Video options.
    /// </summary>
    class VideoOptionsScreen : MenuScreen {
        #region Fields

        MenuEntry back;
        MenuEntry fullScreenEntry;
        MenuEntry colorSchemeEntry;

        const int FULL_SCREEN = 0;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructs a VideoOptionsScreen.
        /// </summary>
        public VideoOptionsScreen() : base("Video Options") {
            fullScreenEntry = new MenuEntry("FullScreen");
            back = new MenuEntry("Back");
            colorSchemeEntry = new MenuEntry("Color Scheme: TODO");

            // Hook up menu event handlers.
            back.Selected += BackSelected;
            
            // Add entries to the menu.
            MenuEntries.Add(fullScreenEntry);
            MenuEntries.Add(colorSchemeEntry);
            MenuEntries.Add(back);

            UpdateText();
        }

        /// <summary>
        /// Updates the text for all the MenuEntries so they reflect the
        /// current values of the options.settings file.
        /// </summary>
        void UpdateText() {
            bool isFullScreen = Options.Default.full_screen;
            String fullScreenText = String.Format("FullScreen: < {0} >", isFullScreen ? "Yes" : "No");

            fullScreenEntry.Text = fullScreenText;
        }

        #endregion

        #region Event Handlers 
        
        /// <summary>
        /// Called upon leaving the VideoOptions. It checks to see there is a 
        /// mismatch between the game being full-screen and the options
        /// dictating if it should or shouldn't. If there is a mismatch, the
        /// game fixes it and then exits the screen.
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// </summary>
        void BackSelected(object sender, PlayerIndexEventArgs e) {
            if (Options.Default.full_screen && !ScreenManager.IsFullScreen()) {
                ScreenManager.ToggleFullScreen();
            }
            else if (!Options.Default.full_screen && ScreenManager.IsFullScreen()) {
                ScreenManager.ToggleFullScreen();
            }

            ExitScreen();
        }

        #endregion

        #region Input Handling

        /// <summary>
        /// Handles the input for the Screen, allowing it to change the fullscreen settings.
        /// </summary>
        /// <param name="input"></param>
        public override void HandleInput(InputState input) {
            int playerIndex = (int)ControllingPlayer.Value;
            PlayerIndex outIndex;

            input.UpdateControllingPlayer(playerIndex);

            if (SelectedEntry == FULL_SCREEN) {
                bool leftRight = input.IsRightButtonPressed() || input.IsLeftButtonPressed();
                bool selected = input.IsMenuSelect(ControllingPlayer, out outIndex);

                if (leftRight || selected) {
                    Options.Default.full_screen = !Options.Default.full_screen;
                }
            }

            Options.Default.Save();
            UpdateText();

            base.HandleInput(input);
        }

        #endregion
    }
}
